axios.interceptors.request.use(req=>{
    req.headers.accesstoken=localStorage.getItem("accessToken");
    return req;
})
axios.interceptors.response.use(resp=>{
    if (resp.headers.accesstoken!=null){
        localStorage.setItem("accessToken",resp.headers.accesstoken)
    }
    return resp;
})
new Vue({
    el: "#app",
    data: {
        productsType: {
            typeName:"",
            typeState:""
        },
        typeId:"",
        updateTypeName:"",
        updateWindow: false,
        addWindow:false,
        addTypeName:""
    },
    methods: {
        handleClose(){
            this.updateWindow=false;
            this.addWindow=false;
        },
        handleSizeChange(pageSize) {
            this.loadAllProductsType(this.productsType.pageIndex, pageSize, this.productsType.typeName, this.productsType.typeState)
        },
        handleCurrentChange(pageIndex) {
            this.loadAllProductsType(pageIndex, this.productsType.pageSize, this.productsType.typeName, this.productsType.typeState)
        },
        loadAllProductsType(pageIndex, pageSize, typeName, typeState) {
            axios.get("/productsType/searchProducts", {
                params: {
                    pageIndex: pageIndex,
                    pageSize: pageSize,
                    typeName: typeName,
                    typeState: typeState
                }
            }).then(resp => {
                console.log(resp)
                this.productsType = resp.data.data
            })
        },
        updateName(typeId) {
            this.updateWindow = true;
            this.typeId = typeId;
        },
        checkUpdateName() {
            axios.post("/productsType/updateProductsTypeName", {
                typeId: this.typeId,
                typeName: this.updateTypeName
            }).then(resp => {
                console.log(resp)
                this.loadAllProductsType(this.productsType.pageIndex,this.productsType.pageSize)
                this.updateWindow = false
                this.updateTypeName = ""
            })
        },
        updateState(typeId, typeState) {
            axios.post("/productsType/updateProductsTypeState", {
                typeId: typeId,
                typeState: typeState
            }).then(resp => {
                console.log(resp)
                alert(resp.data.message)
                this.loadAllProductsType(this.productsType.pageIndex,this.productsType.pageSize)
            })
        },
        add(){
            this.addWindow=true
        },
        checkAdd(){
            axios.post("/productsType/addProductsType",{
                typeName:this.addTypeName
            }).then(resp=>{
                console.log(resp)
                this.addTypeName=""
                this.loadAllProductsType(this.productsType.pageIndex,this.productsType.pageSize)
                this.addWindow=false
            })
        }
        },
        created() {
            this.loadAllProductsType(1, 3)
        }
})